
package com.wyzz.hr.bo.impl;

import java.util.Date;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmpEvalBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.dao.IEmpEvalDao;
import com.wyzz.hr.pojo.Empeval;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.Pager;


public class EmpEvalBoImpl implements IEmpEvalBo {

	public EmpEvalBoImpl() {
	}

	public List findEvalByEmpNo(String empNo) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empeval.class);
		detachedCriteria.setFetchMode(Empeval.PROP_EMPLOYEE_BY_EE_EMP_NO,
				FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empeval.PROP_EMPLOYEE_BY_EE_MANAGER,
				FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empeval.PROP_DEPARTMENT, FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empeval.PROP_EE_PB_NO, FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq((new StringBuilder()).append(
				Empeval.PROP_EMPLOYEE_BY_EE_EMP_NO).append(".").append(
				Employee.PROP_ID).toString(), empNo));
		detachedCriteria.addOrder(Order.desc(Empeval.PROP_EE_START_DATE));
		List result = empEvalDao.findByCriteria(detachedCriteria);
		return result;
	}

	public List findEval(DetachedCriteria dc, Pager page, String searchOrExport) {
		if (page == null || "export".equals(searchOrExport)) {
			return empEvalDao.findByCriteria(dc);
		} else {
			page.splitPage(dc);
			return empEvalDao.findByCriteria(dc, page.getPageSize(), page
					.getCurrentPage());
		}
	}

	public Empeval getById(String evalId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empeval.class);
		detachedCriteria.setFetchMode(Empeval.PROP_EMPLOYEE_BY_EE_EMP_NO,
				FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empeval.PROP_EMPLOYEE_BY_EE_MANAGER,
				FetchMode.JOIN);
		detachedCriteria.setFetchMode(Empeval.PROP_DEPARTMENT, FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq(Empeval.PROP_EE_ID, evalId));
		List result = empEvalDao.findByCriteria(detachedCriteria);
		if (result.size() == 0)
			return null;
		else
			return (Empeval) result.get(0);
	}

	public boolean insertEval(Empeval empEval) {
		try {
			empEvalDao.saveObject(empEval);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public String insert(Empeval empEval, Employee currentEmp) {
		empEvalDao.saveObject(empEval);
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		Employee employee = employeeBo.loadEmp(empEval.getEmployeeByEeEmpNo()
				.getId(), null);
		employee.setEmpPerfRate(empEval.getEeRate());
		employee.setEmpPerfDate(new Date());
		employee.setEmpLastChangeTime(new Date());
		employee.setEmpLastChangeBy(currentEmp);
		empEvalDao.updateObject(employee);
		return empEval.getEeId();
	}

	public void delete(String eeId) {
		String hql = (new StringBuilder()).append(
				"delete from Empeval where eeId='").append(eeId).append("'")
				.toString();
		empEvalDao.exeHql(hql);
	}

	public void update(Empeval empEval, String EvalId, Employee currentEmp) {
		Empeval oldEval = getById(EvalId);
		if (empEval.getEeAttachment() != null) {
			if (oldEval != null)
				FileOperate.deleteFile("sys.profile.file.path", oldEval
						.getEeAttachment());
			oldEval.setEeAttachment(empEval.getEeAttachment());
		} else if (oldEval != null)
			empEval.setEeAttachment(oldEval.getEeAttachment());
		oldEval.setEeComments(empEval.getEeComments());
		oldEval.setEeRate(empEval.getEeRate());
		oldEval.setEmployeeByEeManager(empEval.getEmployeeByEeManager());
		oldEval.setEeLastChangeBy(empEval.getEeLastChangeBy());
		oldEval.setEeLastChangeTime(new Date());
		oldEval.setEeLastChangeTime(empEval.getEeLastChangeTime());
		empEvalDao.saveOrupdate(oldEval);
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		Employee employee = employeeBo.loadEmp(oldEval.getEmployeeByEeEmpNo()
				.getId(), null);
		employee.setEmpPerfRate(empEval.getEeRate());
		employee.setEmpPerfDate(new Date());
		employee.setEmpLastChangeBy(currentEmp);
		employee.setEmpLastChangeTime(new Date());
	}

	public boolean deleteAttach(String evalId, String fileName) {
		try {
			FileOperate.deleteFile("sys.profile.file.path", fileName);
			String hql = (new StringBuilder()).append(
					"update Empeval set eeAttachment=null where eeId='")
					.append(evalId).append("'").toString();
			empEvalDao.exeHql(hql);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public int repeatInTimeNum(Empeval empeval) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empeval.class);
		detachedCriteria.add(Restrictions.eq(
				Empeval.PROP_EMPLOYEE_BY_EE_EMP_NO, empeval
						.getEmployeeByEeEmpNo()));
		detachedCriteria.add(Restrictions.eq(Empeval.PROP_EE_TYPE, empeval
				.getEeType()));
		detachedCriteria.add(Restrictions.eq(Empeval.PROP_EE_START_DATE,
				DateUtil.convDateTimeToDate(empeval.getEeStartDate())));
		detachedCriteria.add(Restrictions.eq(Empeval.PROP_EE_END_DATE, DateUtil
				.convDateTimeToDate(empeval.getEeEndDate())));
		int result = empEvalDao.findRowCountByCriteria(detachedCriteria);
		return result;
	}

	public Empeval getEmpevalInTest(String empDistinctNo) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empeval.class);
		detachedCriteria.add(Restrictions.eq(
				Empeval.PROP_EMPLOYEE_BY_EE_EMP_NO, empDistinctNo));
		detachedCriteria.add(Restrictions.eq(Empeval.PROP_EE_TYPE,
				"\u8BD5\u7528\u671F"));
		List evalList = empEvalDao.findByCriteria(detachedCriteria);
		if (evalList.size() > 0)
			return (Empeval) evalList.get(0);
		else
			return null;
	}

	public boolean hasPprobationaryPeriodEvalRecord(String empId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empeval.class);
		detachedCriteria.add(Restrictions.eq("employeeByEeEmpNo.id", empId));
		detachedCriteria.add(Restrictions.eq("eeType", "\u8BD5\u7528\u671F"));
		return empEvalDao.findByCriteria(detachedCriteria).size() > 0;
	}

	public IEmpEvalDao getEmpEvalDao() {
		return empEvalDao;
	}

	public void setEmpEvalDao(IEmpEvalDao empEvalDao) {
		this.empEvalDao = empEvalDao;
	}

	IEmpEvalDao empEvalDao;
}
